package fr.SDroid.Onglet;

import java.util.HashMap;
import java.util.Map.Entry;

import fr.SDroid.R;
import fr.SDroid.Graph.Graph;
import fr.SDroid.Graph.Station;
import fr.SDroid.TranSpeed;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

public class Search extends Activity{
		Graph graph;
	
	 public void onCreate(Bundle savedInstanceState) {
    	 super.onCreate(savedInstanceState);
         setContentView(R.layout.search);
         
         ImageView back = (ImageView)findViewById(R.id.ImageView01);
         back.setBackgroundColor(Color.parseColor("#333333"));
         
 //        graph = (Graph) getIntent().getBundleExtra("object").getSerializable("graph");
         graph = TranSpeed.getGraph();
         
         Spinner spinnerDeb = (Spinner) findViewById(R.id.SpiChoixDeb);
         final AutoCompleteTextView textDebStat = (AutoCompleteTextView) findViewById(R.id.AutoTextDebut);

         
 //        Spinner spinnerFin = (Spinner) findViewById(R.id.SpiChoixFin);
         final AutoCompleteTextView textFinStat = (AutoCompleteTextView) findViewById(R.id.AutoTextFin);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         adapter.add("Station");
       
         
         spinnerDeb.setAdapter(adapter);
         
         ArrayAdapter<String> adapterStation = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
         HashMap<String, Station> unique = new HashMap<String, Station>();
         for(int ligne = 0; ligne < graph.getLignes().size(); ligne++) {
        	 for(int station = 0; station < graph.getLignes().get(ligne).getListStation().size(); station++) {
        		 Station statTemp = graph.getLignes().get(ligne).getListStation().get(station);
        		 unique.put(statTemp.getName(), statTemp);
        	}
         }
         
         for(Entry<String, Station> entry : unique.entrySet()) {
        	 adapterStation.add(entry.getKey());
         }
         
         textDebStat.setAdapter(adapterStation);
         textFinStat.setAdapter(adapterStation);
         
         Button start = (Button) findViewById(R.id.Button01);
         start.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String debut = textDebStat.getText().toString().toLowerCase();
				String arrive = textFinStat.getText().toString().toLowerCase();
				
				Station stationDebut = null;
				Station stationFin = null;
				
				// verification des stations
				for(int i = 0; i < graph.getLignes().size(); i++) {
					for(int j = 0; j < graph.getLignes().get(i).getListStation().size(); j++) {
						Station station = graph.getLignes().get(i).getListStation().get(j);
						if(station.getName().toLowerCase().equals(debut))
							stationDebut = station;
						if(station.getName().toLowerCase().equals(arrive))
							stationFin = station;
					}
				}
				
				if(stationDebut == null || stationFin == null) {
					alert();
				}
				else {
					graph.setStationDepart(stationDebut);
					graph.setStationArrive(stationFin);
					startCalcul();
				}
				
			}
		});
         
	 }
	 
	 private void alert() {
		 AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	    alertDialog.setTitle("Error");

	    alertDialog.setMessage("Wrong name of station");
	    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
	    alertDialog.show();  
	 }
	 
	 private void startCalcul() {
		 Intent intentCalcul = new Intent(this, Calcul.class);
		 startActivity(intentCalcul);
	 }
}
